package topin.facecat.FaceCat

import android.Manifest
import android.content.pm.ActivityInfo
import android.content.pm.PackageManager
import android.content.res.Configuration
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import facecat.topin.sock.FCClientSocket
import facecat.topin.xml.FCUIXml
import topin.facecat.FaceCat.Android.FCUIView

/*
* 主视图
*/
class MainActivity : AppCompatActivity() {
    /**
     * 创建视图
     * @param savedInstanceState
     */
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (m_mainActivity != null && m_mainActivity !== this) {
            finish()
            return
        }
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        m_mainActivity = this
        if (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            ) == PackageManager.PERMISSION_DENIED
        ) {
            ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
                1
            )
        }
        if (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.READ_EXTERNAL_STORAGE
            ) == PackageManager.PERMISSION_DENIED
        ) {
            ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE),
                1
            )
        }
        if (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.CAMERA
            ) == PackageManager.PERMISSION_DENIED
        ) {
            ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.CAMERA),
                1
            )
        }
        FCClientSocket.noThread = false
        val view = FCUIView(baseContext)
        view.setAllowScroll(false)
        view.onLoad()
        val mainFrame = FCUIXml()
        mainFrame.native = view.native
        mainFrame.loadXml("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<html xmlns=\"facecat\">\n" +
                "  <body>\n" +
                "    <div dock=\"fill\" backcolor=\"rgb(140,192,81)\">\n" +
                "      <div name=\"Div\" text=\"Div\" location=\"368,127\" size=\"247,326\" align=\"center\" vertical-align=\"middle\" backcolor=\"rgb(170,178,189)\" cornerradius=\"10\">\n" +
                "        <input type=\"text\" name=\"TextBox\" location=\"14,18\" size=\"220,45\" cornerradius=\"0\" bordercolor=\"rgb(0,0,0)\" backcolor=\"rgb(42,48,72)\" font=\"Default,30.0\"/>\n" +
                "        <input type=\"button\" name=\"btnAC\" text=\"AC\" location=\"14,69\" size=\"55,48\" font=\"Default,18.0\" backcolor=\"rgb(62,72,95)\" bordercolor=\"rgb(0,0,0)\"/>\n" +
                "        <input type=\"button\" name=\"btnAddSub\" text=\"+/-\" location=\"69,69\" size=\"55,48\" font=\"Default,18.0\" backcolor=\"rgb(62,72,95)\" bordercolor=\"rgb(0,0,0)\"/>\n" +
                "        <input type=\"button\" name=\"btnMod\" text=\"%\" location=\"124,69\" size=\"55,48\" font=\"Default,18.0\" backcolor=\"rgb(62,72,95)\" bordercolor=\"rgb(0,0,0)\"/>\n" +
                "        <input type=\"button\" name=\"btnDivide\" text=\"÷\" location=\"179,69\" size=\"55,48\" font=\"Default,18.0\" backcolor=\"rgb(254,159,11)\" bordercolor=\"rgb(0,0,0)\"/>\n" +
                "        <input type=\"button\" name=\"btnNum7\" text=\"7\" location=\"14,117\" size=\"55,48\" font=\"Default,18.0\" backcolor=\"rgb(93,103,121)\" bordercolor=\"rgb(0,0,0)\"/>\n" +
                "        <input type=\"button\" name=\"btnNum8\" text=\"8\" location=\"69,117\" size=\"55,48\" font=\"Default,18.0\" backcolor=\"rgb(93,103,121)\" bordercolor=\"rgb(0,0,0)\"/>\n" +
                "        <input type=\"button\" name=\"btnNum9\" text=\"9\" location=\"124,117\" size=\"55,48\" font=\"Default,18.0\" backcolor=\"rgb(93,103,121)\" bordercolor=\"rgb(0,0,0)\"/>\n" +
                "        <input type=\"button\" name=\"btnMulti\" text=\"×\" location=\"179,117\" size=\"55,48\" font=\"Default,18.0\" backcolor=\"rgb(254,159,11)\" bordercolor=\"rgb(0,0,0)\"/>\n" +
                "        <input type=\"button\" name=\"btnNum4\" text=\"4\" location=\"14,165\" size=\"55,48\" font=\"Default,18.0\" backcolor=\"rgb(93,103,121)\" bordercolor=\"rgb(0,0,0)\"/>\n" +
                "        <input type=\"button\" name=\"btnNum5\" text=\"5\" location=\"69,165\" size=\"55,48\" font=\"Default,18.0\" backcolor=\"rgb(93,103,121)\" bordercolor=\"rgb(0,0,0)\"/>\n" +
                "        <input type=\"button\" name=\"btnNum6\" text=\"6\" location=\"124,165\" size=\"55,48\" font=\"Default,18.0\" backcolor=\"rgb(93,103,121)\" bordercolor=\"rgb(0,0,0)\"/>\n" +
                "        <input type=\"button\" name=\"btnSub\" text=\"-\" location=\"179,165\" size=\"55,48\" font=\"Default,18.0\" backcolor=\"rgb(254,159,11)\" bordercolor=\"rgb(0,0,0)\"/>\n" +
                "        <input type=\"button\" name=\"btnNum1\" text=\"1\" location=\"14,213\" size=\"55,48\" font=\"Default,18.0\" backcolor=\"rgb(93,103,121)\" bordercolor=\"rgb(0,0,0)\"/>\n" +
                "        <input type=\"button\" name=\"btnNum2\" text=\"2\" location=\"69,213\" size=\"55,48\" font=\"Default,18.0\" backcolor=\"rgb(93,103,121)\" bordercolor=\"rgb(0,0,0)\"/>\n" +
                "        <input type=\"button\" name=\"btnNum3\" text=\"3\" location=\"124,213\" size=\"55,48\" font=\"Default,18.0\" backcolor=\"rgb(93,103,121)\" bordercolor=\"rgb(0,0,0)\"/>\n" +
                "        <input type=\"button\" name=\"btnAdd\" text=\"+\" location=\"179,213\" size=\"55,48\" font=\"Default,18.0\" backcolor=\"rgb(254,159,11)\" bordercolor=\"rgb(0,0,0)\"/>\n" +
                "        <input type=\"button\" name=\"btnNum0\" text=\"0\" location=\"14,261\" size=\"110,48\" font=\"Default,18.0\" backcolor=\"rgb(93,103,121)\" bordercolor=\"rgb(0,0,0)\"/>\n" +
                "        <input type=\"button\" name=\"btnDot\" text=\".\" location=\"124,261\" size=\"55,48\" font=\"Default,18.0\" backcolor=\"rgb(93,103,121)\" bordercolor=\"rgb(0,0,0)\"/>\n" +
                "        <input type=\"button\" name=\"btnResult\" text=\"=\" location=\"179,261\" size=\"55,48\" font=\"Default,18.0\" backcolor=\"rgb(254,159,11)\" bordercolor=\"rgb(0,0,0)\"/>\n" +
                "      </div>\n" +
                "    </div>\n" +
                "  </body>\n" +
                "</html>", null)
        //view.setLoadUI(m_chatMainFrame);
        setContentView(view)
        mainFrame.native.update()
        mainFrame.native.invalidate()
    }

    /**
     * 回退
     */
    override fun onBackPressed() {
        moveTaskToBack(true)
    }

    /**
     * 配置改变
     * @param newConfig
     */
    override fun onConfigurationChanged(newConfig: Configuration) {
        // TODO Auto-generated method stub
        try {
            super.onConfigurationChanged(newConfig)
            if (resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            } else if (resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT) {
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    companion object {
        /*
        * 主视图静态对象
         */
        var m_mainActivity: MainActivity? = null
    }
}